home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
drdobbs
/
c_spec
/
execute
/
ptext.c
< prev
next >
Wrap
Text File
|
1986-02-20
|
4KB
|
163 lines
#include <stdio.h>
/*
* PTEXT.C: Multi-column print utility
*
* Copyright (c) 1985 Allen I. Holub, all rights reserved.
* This program may be reproduced for personal, non-profit use only
*
* Bugs and Features:
*
* 1) Pr_line assumes that the output device supports backspace
* ('\b' == ^H ). This is only a problem when the source
* has underlined text implemented as:
* texttexttext\r _______
* We can't just ouput a '\r' in this case because we may
* not be in the leftmost column. A \r should get us to the
* left edge of the current column.
*
* 2) When printing multi-column stuff. If a line is truncated it
* will run into the column to its right. That is, there is no
* seperator between columns other than the whitespace needed
* to pad the column out to a particular width. If no padding
* is necessary (ie. the lines have been truncated), then the
* columns will run together.
*
* 3) When an ESC is found, the escape and the next three characters
* take up no space in the output. This lets us print out the
* various SGR commands without messing up the column width.
*
* 4) Strange things happen to an IBM screen when the leftmost
* character on a line is printed with underline. To compensate
* for this, a single blank is printed as the left-most
* character on every line if IBM is #defined.
*/
#define ESC 0x1b
#define IBM 1
/*----------------------------------------------------------------------*/
ptext(linec, linev, outfile, numcols, colwidth, numrows)
int linec, numcols, colwidth;
char **linev;
FILE *outfile;
int numrows;
{
/* Print out the array of strings "linev" which consists of
* linec entrys. Output is sent to "outfile" formated as
* follows:
*
* "numcols" : number of columns
* "colwidth" : width of a column in characters. Any text
* longer than colwidth is truncated off.
* "numrows" : columns are numrows long. The left-most
* column is printed in its entirety
* first then the next column in its
* entirety, and so on.
*/
register int j ;
register char **lineend, **line, **nextline ;
lineend = &linev[linec-1]; /* Last linev entry to print */
for( j = numrows ; --j >= 0 ; )
{
#ifdef IBM
putc( ' ', outfile );
#endif
for( line = linev++; line <= lineend; line = nextline)
{
nextline = line + numrows;
/* Print the line. Don't pad the rightmost */
/* column. */
pr_line( *line, outfile, colwidth,
nextline <= lineend);
}
fputs("\n" , outfile);
}
}
/*----------------------------------------------------------------------*/
static pr_line( str, stream, width, padded )
register char *str;
FILE *stream;
int width, padded;
{
/* Print out "str" into "stream" padding it to "width"
* columns wide. Non-printing characters and 3 printing characters
* immediatly following an ESC are not counted as having printed.
* '\n' characters are treated as line teminators but are not
* printed. If "padded" is 0 then no padding is done, though the
* line will still be truncated if it's too long.
*/
int col = 0 ;
while( col < width && *str )
{
if ( *str == '\n' )
break;
else if( *str == '\r' )
{
/* Back up to the left edge of the current column
*/
while( col > 0 )
{
--col;
putc('\b' , stream);
}
str++;
}
else if( *str == '\t' ) /* expand tabs */
{
str++ ;
col++ ;
putc( ' ' , stream );
while( (col % 8) && col < width)
{
putc( ' ' , stream );
col++;
}
}
else
{
if( *str == ESC )
{
putc( *str++ , stream );
if( !*str )
break;
putc( *str++ , stream );
if( !*str )
break;
putc( *str++ , stream );
if( !*str )
break;
}
else if (*str == '\b')
--col ;
else if( *str >= ' ' )
++col ;
putc( *str++ , stream );
}
}
if (padded)
while( col++ < width )
putc( ' ' , stream );
}